package com.youyi.yyscreencutlibrary.SDK;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.Point;
import android.graphics.Rect;
import android.os.Build;
import android.view.Display;
import android.view.WindowManager;
import com.google.gson.Gson;

import com.youyi.yyscreencutlibrary.ColoPicker.YYColorPickerActivity;
import com.youyi.yyscreencutlibrary.Core.YYScreenCutActivity;

public class YYScreenCutSDK {
    public static int PixHeight;
    public static int PixWidth;
    public static int StarBarHeight;
    public static Intent data;
    public static boolean hasPermission = false;
    public static Bitmap mColorBitmap;
    private static volatile boolean mHasCheckAllScreen;
    private static volatile boolean mIsAllScreenDevice;
    public static boolean mIsFinish;
    public static CutType mNowCutType;
    public static Rect mNowMarkRect;
    public static Rect mNowRect;
    public static OnColorPickListener mOnColorPickListener;
    public static OnCutListener mOnCutListener;
    public static OnRectListener mOnRectListener;
    private static final YYScreenCutSDK ourInstance = new YYScreenCutSDK();
    public static int resultCode;
    private Intent mIntent;
    public OnScreenCutDestroyListener mOnScreenCutDestroyListener;

    public enum CutType {
        CutFull,
        CutByRect,
        CutByData,
        GetRectData
    }

    public interface OnColorPickListener {
        void result(boolean z, int i, int i2, String str);
    }

    public interface OnCutListener {
        void result(boolean z, Bitmap bitmap);
    }

    public interface OnRectListener {
        void result(boolean z, Rect rect);
    }

    public interface OnScreenCutDestroyListener {
        void result(boolean z);
    }

    public void setOnScreenCutDestroyListener(OnScreenCutDestroyListener onScreenCutDestroyListener) {
        this.mOnScreenCutDestroyListener = onScreenCutDestroyListener;
    }

    public static YYScreenCutSDK getInstance() {
        return ourInstance;
    }

    private YYScreenCutSDK() {
    }

    public void cutFull(Context context, OnCutListener onCutListener) {
        mIsFinish = false;
        mOnCutListener = onCutListener;
        Intent intent = new Intent(context, YYScreenCutActivity.class);
        this.mIntent = intent;
        intent.putExtra("cutType", CutType.CutFull.toString());
        this.mIntent.addFlags(335544320);
        context.startActivity(this.mIntent);
    }

    public void cutRect(Context context, Rect rect, OnCutListener onCutListener) {
        mIsFinish = false;
        mOnCutListener = onCutListener;
        Intent intent = new Intent(context, YYScreenCutActivity.class);
        this.mIntent = intent;
        intent.putExtra("cutType", CutType.CutByRect.toString());
        this.mIntent.putExtra("rectJson", new Gson().toJson(rect));
        this.mIntent.addFlags(335544320);
        context.startActivity(this.mIntent);
    }

    public void cutByData(Context context, Rect rect, OnCutListener onCutListener) {
        mIsFinish = false;
        mOnCutListener = onCutListener;
        Intent intent = new Intent(context, YYScreenCutActivity.class);
        this.mIntent = intent;
        intent.putExtra("cutType", CutType.CutByData.toString());
        if (rect != null) {
            this.mIntent.putExtra("rectJson", new Gson().toJson(rect));
        }
        this.mIntent.addFlags(335544320);
        context.startActivity(this.mIntent);
    }

    public void getRectData(Context context, Rect rect, OnRectListener onRectListener) {
        mIsFinish = false;
        mOnRectListener = onRectListener;
        Intent intent = new Intent(context, YYScreenCutActivity.class);
        this.mIntent = intent;
        intent.putExtra("cutType", CutType.GetRectData.toString());
        if (rect != null) {
            this.mIntent.putExtra("rectJson", new Gson().toJson(rect));
        }
        this.mIntent.addFlags(335544320);
        context.startActivity(this.mIntent);
    }

    public void pickColor(final Context context, final OnColorPickListener onColorPickListener) {
        mIsFinish = false;
        cutFull(context, new OnCutListener() {
            /* class com.youyi.yyscreencutlibrary.SDK.YYScreenCutSDK.AnonymousClass1 */

            @Override // com.youyi.yyscreencutlibrary.SDK.YYScreenCutSDK.OnCutListener
            public void result(boolean z, Bitmap bitmap) {
                if (z) {
                    YYScreenCutSDK.mColorBitmap = bitmap;
                    YYScreenCutSDK.mOnColorPickListener = onColorPickListener;
                    YYScreenCutSDK.this.mIntent = new Intent(context, YYColorPickerActivity.class);
                    YYScreenCutSDK.this.mIntent.addFlags(335544320);
                    context.startActivity(YYScreenCutSDK.this.mIntent);
                    return;
                }
                YYScreenCutSDK.mColorBitmap = null;
                if (onColorPickListener != null) {
                    onColorPickListener.result(false, 0, 0, null);
                }
            }
        });
    }

    public boolean isAllScreenDevice(Context context) {
        float f;
        int i;
        if (mHasCheckAllScreen) {
            return mIsAllScreenDevice;
        }
        mHasCheckAllScreen = true;
        mIsAllScreenDevice = false;
        if (Build.VERSION.SDK_INT < 21) {
            return false;
        }
        WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        if (windowManager != null) {
            Display defaultDisplay = windowManager.getDefaultDisplay();
            Point point = new Point();
            defaultDisplay.getRealSize(point);
            if (point.x < point.y) {
                f = (float) point.x;
                i = point.y;
            } else {
                f = (float) point.y;
                i = point.x;
            }
            if (((float) i) / f >= 1.97f) {
                mIsAllScreenDevice = true;
            }
        }
        return mIsAllScreenDevice;
    }

    public static void setYunPhoneFlag(Context context, boolean z) {
        context.getSharedPreferences("VoiceData_1", 0).edit().putBoolean("setYunPhoneFlag", z).commit();
    }

    public static boolean getYunPhoneFlag(Context context) {
        return context.getSharedPreferences("VoiceData_1", 0).getBoolean("setYunPhoneFlag", false);
    }

    public static String getColorByXY(Bitmap bitmap, int i, int i2, boolean z) {
        if (bitmap == null) {
            return null;
        }
        int pixel = bitmap.getPixel(i, i2);
        String hexString = Integer.toHexString(Color.red(pixel));
        String hexString2 = Integer.toHexString(Color.green(pixel));
        String hexString3 = Integer.toHexString(Color.blue(pixel));
        if (hexString.length() == 1) {
            hexString = "0" + hexString;
        }
        if (hexString2.length() == 1) {
            hexString2 = "0" + hexString2;
        }
        if (hexString3.length() == 1) {
            hexString3 = "0" + hexString3;
        }
        if (z) {
            bitmap.recycle();
        }
        return (hexString + hexString2 + hexString3).toUpperCase();
    }

    public static boolean hasColor(Bitmap bitmap, String str) {
        if (str.contains("#")) {
            str = str.replaceAll("#", "");
        }
        if (bitmap == null) {
            return false;
        }
        boolean z = false;
        for (int i = 0; i < bitmap.getHeight(); i++) {
            int i2 = 0;
            while (true) {
                if (i2 >= bitmap.getWidth()) {
                    break;
                }
                int pixel = bitmap.getPixel(i2, i);
                String hexString = Integer.toHexString(Color.red(pixel));
                String hexString2 = Integer.toHexString(Color.green(pixel));
                String hexString3 = Integer.toHexString(Color.blue(pixel));
                if (hexString.length() == 1) {
                    hexString = "0" + hexString;
                }
                if (hexString2.length() == 1) {
                    hexString2 = "0" + hexString2;
                }
                if (hexString3.length() == 1) {
                    hexString3 = "0" + hexString3;
                }
                if (str.equals((hexString + hexString2 + hexString3).toUpperCase())) {
                    z = true;
                    break;
                }
                i2++;
            }
        }
        return z;
    }

    public static boolean isScreenPortrait(Context context) {
        return context.getResources().getConfiguration().orientation != 2;
    }

    public void destroyScreenCut() {
        OnScreenCutDestroyListener onScreenCutDestroyListener = this.mOnScreenCutDestroyListener;
        if (onScreenCutDestroyListener != null) {
            onScreenCutDestroyListener.result(true);
        }
    }
}
